//
//  HElCourseIntroVC.m
//  NursingAssociation
//
//  Created by wsj2015 on 2017/2/16.
//  Copyright © 2017年 wsj2015. All rights reserved.
//

#import "HElCourseIntroVC.h"
#import "UIImageView+BTimageView.h"
#import "HELSepcialistDetailVC.h"
#import "DGSAttributedStringSuite.h"
#import "UILabel+labelTextContent.h"
#import "HELCourseContentsVC.h"
#import "HECourseDetailScrollView.h"
#import "WSJTipTool.h"

@interface HElCourseIntroVC ()<UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UILabel *introContent;
@property (weak, nonatomic) IBOutlet HECourseDetailScrollView *scrollView;

@property (weak, nonatomic) IBOutlet UILabel *suitPeople;

@property (weak, nonatomic) IBOutlet UIImageView *avrtarImg;
@property (weak, nonatomic) IBOutlet UILabel *namelL;
@property (weak, nonatomic) IBOutlet UILabel *skillsL;

@property (nonatomic,  assign) BOOL isExpend;

@property (nonatomic,  assign) BOOL isExpend2;

@property (weak, nonatomic) IBOutlet UIView *tapView;

@property (nonatomic,  assign) BOOL canScroll;


@end

@implementation HElCourseIntroVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isExpend = NO;
    
    self.isExpend2 = NO;

  
    // Do any additional setup after loading the view from its nib.
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAvarta)];
    self.avrtarImg.layer.anchorPoint = CGPointMake(0.5, 0.5);
    [self.avrtarImg addGestureRecognizer:tap];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(acceptMsg:) name:kGoTopNotificationName object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(acceptMsg:) name:kLeaveTopNotificationName object:nil];//其中一个TAB离开顶部的时候，如果其他几个偏移量不为0的时候，要把他们都置为0
    
    [WSJTipTool showTipForFunction:@"tap_specilist" withWords:@"点我试试~  进入专家介绍哦~" withView:self.avrtarImg.superview fromFrame:self.avrtarImg.frame direction:AMPopTipDirectionUp];
    
    
   
    
 
    
   
  
    
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


-(void)acceptMsg : (NSNotification *)notification{
    //NSLog(@"%@",notification);
    NSString *notificationName = notification.name;
    if ([notificationName isEqualToString:kGoTopNotificationName]) {
        NSDictionary *userInfo = notification.userInfo;
        NSString *canScroll = userInfo[@"canScroll"];
        if ([canScroll isEqualToString:@"1"]) {
            self.canScroll = YES;
            self.scrollView.showsVerticalScrollIndicator = YES;
        }
    }else if([notificationName isEqualToString:kLeaveTopNotificationName]){
        self.scrollView.contentOffset = CGPointZero;
        self.canScroll = NO;
        self.scrollView.showsVerticalScrollIndicator = NO;
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (!self.canScroll) {
        [scrollView setContentOffset:CGPointZero];
    }
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY<0) {
        [[NSNotificationCenter defaultCenter] postNotificationName:kLeaveTopNotificationName object:nil userInfo:@{@"canScroll":@"1"}];
    }
}
- (void)tapAvarta{
    
    [WSJTipTool hideWithPopWithSupview:self.avrtarImg.superview];
    
    if (!self.self.courseDetail.lecturerId) {
        return;
    }
    HELSepcialistDetailVC *vc =[HELSepcialistDetailVC new];
    vc.specialistId = [self.courseDetail.lecturerId integerValue];
    [self.parentViewController.navigationController pushViewController:vc animated:YES];

}


- (void)expendSkillLabel{
    self.isExpend2 = !self.isExpend2;
    self.courseDetail = self.courseDetail;
    if (self.isExpend2) {
        self.skillsL.numberOfLines = 0;
    }else{
        self.skillsL.numberOfLines = 4;
    }
    [UIView animateWithDuration:.35 animations:^{
        
        [self.scrollview layoutIfNeeded];
        
    }];
  
    
}

- (void)expendIntroLabel{
    self.isExpend = !self.isExpend;
    self.courseDetail = self.courseDetail;
    if (self.isExpend) {
        self.introContent.numberOfLines = 0;
    }else{
        self.introContent.numberOfLines = 3;
    }
    [UIView animateWithDuration:.35 animations:^{
        
        [self.scrollview layoutIfNeeded];
        
    }];
    
    
}

- (void)setCourseDetail:(HELCourseDetailModel *)courseDetail{
    _courseDetail = courseDetail;
    
    self.introContent.text= courseDetail.courseAbstract;
    [self.introContent changeSpacewithLineSpace:6 WordSpace:1];
    self.suitPeople.text = courseDetail.suitableCrowd;
    [self.avrtarImg nc_setImageWithUrl:courseDetail.lecturerImageUrl];
    self.namelL.text = courseDetail.lecturerName;
    self.skillsL.text = courseDetail.lecturerIntroduce;
    [self.suitPeople changeSpacewithLineSpace:6 WordSpace:1];
    [self.skillsL changeSpacewithLineSpace:6 WordSpace:1];
    [self.view layoutIfNeeded];
    
    
    NSArray *lineArr = [self.introContent getLinesArrayOfStringInLabel];
    if (lineArr.count>3) {
        [self.introContent expendWithIsexpend:self.isExpend withDefaultNumberOfline:3];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(expendIntroLabel)];
        [self.tapView addGestureRecognizer:tap];
    }else{
        [self.tapView.gestureRecognizers enumerateObjectsUsingBlock:^(__kindof UIGestureRecognizer * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [self.tapView removeGestureRecognizer:obj];
        }];
    }
    
    
    NSArray *lineArr2 = [self.skillsL getLinesArrayOfStringInLabel];
    if (lineArr2.count>4) {
        [self.skillsL expendWithIsexpend:self.isExpend2 withDefaultNumberOfline:4];
        UITapGestureRecognizer *tapSkill = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(expendSkillLabel)];
        [self.skillsL addGestureRecognizer:tapSkill];
    }else{
        [self.skillsL.gestureRecognizers enumerateObjectsUsingBlock:^(__kindof UIGestureRecognizer * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [self.skillsL removeGestureRecognizer:obj];
        }];
    }
 
    
}

- (void)viewDidLayoutSubviews{
    
    [super viewDidLayoutSubviews];
  

}

@end
